from ChessConsts import ChessConsts

def getValidRookMoves(self,fromPos):
# darren & ronan work in progress

	moves = []        
	dirs = [(1,0),(-1,0),(0,1),(0,-1)]

	moves = self.traceValidMoves(fromPos,dirs)

	moves = checkKingGuard(self, fromPos,moves)

	return moves

def getValidPawnMoves(board, fromPos):
        '''gcoghlan && oxana - working on this one'''
        
        moves = []
        specialMoves = {}
        fx, fy = fromPos
        
        if board._turn == ChessConsts.WHITE:
            movedir = -1
            startrow = 6
            ocol = ChessConsts.BLACK
            eprow = 3
        else:
            movedir = 1
            startrow = 1
            ocol = ChessConsts.WHITE
            eprow = 4
        
        if board.isFree(fx, fy + movedir):
            moves.append((fx, fy + movedir))
            
        if fy == startrow:
            if board.isFree(fx, fy + movedir) and board.isFree(fx, fy + (movedir * 2)):
                moves.append((fx, fy + (movedir * 2)))
                specialMoves[(fx, fy + (movedir * 2))] = board.EP_MOVE
        if fx < 7 and board.getColor(fx + 1, fy + movedir) == ocol:
            moves.append((fx + 1, fy + movedir))
        if fx > 0 and board.getColor(fx - 1, fy + movedir) == ocol:
            moves.append((fx - 1, fy + movedir))
            
        if fy == eprow and board._ep[1] != 0:
            if board._ep[0] == fx + 1:
               moves.append((fx + 1, fy + movedir))
               specialMoves[(fx + 1, fy + movedir)] = board.EP_CAPTURE_MOVE
            if board._ep[0] == fx - 1:
               moves.append((fx - 1, fy + movedir))
               specialMoves[(fx - 1, fy + movedir)] = board.EP_CAPTURE_MOVE

        moves = checkKingGuard(board, fromPos, moves, specialMoves)
         
        return (moves, specialMoves)

# Sue and Zaid

def checkKingGuard(chessboard,fromPos,moves,specialMoves={}):
	if chessboard._turn == ChessConsts.WHITE:
		kx,ky = chessboard._white_king_location
	else:
		kx,ky = chessboard._black_king_location

	fx,fy = fromPos

  	done = False
        fp = chessboard._board[fy][fx]
        chessboard._board[fy][fx] = "."
        if not chessboard.isThreatened(kx,ky):
        	done = True
        chessboard._board[fy][fx] = fp

        if done:
        	return moves
	
	return __getValidKingGuardMovements(chessboard,moves,specialMoves,fx,fy,kx,ky)

def __getValidKingGuardMovements(chessboard,moves,specialMoves,fx,fy,kx,ky):
	result = []
        for m in moves:
        	tx,ty = m
            	sp = None
            	fp = chessboard._board[fy][fx]
            	tp = chessboard._board[ty][tx]

           	chessboard._board[fy][fx] = "."
            	chessboard._board[ty][tx] = fp

            	if specialMoves.has_key(m) and specialMoves[m] == chessboard.EP_CAPTURE_MOVE:
                	sp = chessboard._board[chessboard._ep[1]][chessboard._ep[0]]
                	chessboard._board[chessboard._ep[1]][chessboard._ep[0]] = "."

            	if not chessboard.isThreatened(kx,ky):
                	result.append(m)

            	if sp:
                	chessboard._board[chessboard._ep[1]][chessboard._ep[0]] = sp

            	chessboard._board[fy][fx] = fp
            	chessboard._board[ty][tx] = tp

        return result

    # Kerill and Alan
def getValidBishopMoves(self,fromPos):
        moves = []        
        dirs = [(1,1),(-1,1),(1,-1),(-1,-1) ]
        
        moves = self.traceValidMoves(fromPos,dirs)
        
        moves = self.checkKingGuard(fromPos,moves)
        
        return moves

#Christine (CLynch)
def threeRepetitions(chessboard):
    ts = chessboard._three_rep_stack[:chessboard._state_stack_pointer]
    if (len(ts) and (ts.count(ts[len(ts)-1]) == 3)):
        return True
    return False